package net;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.apache.poi.ss.usermodel.Workbook;
import util.ExcelUtils;
import util.SightRegex;
import util.Spider;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.util.*;

/**
 * @fileName: DianpingGrab
 * @author: Han
 * @date: 2018/5/24 14:58
 * @dscription:
 */
public class DianpingGrab {

    public static void main(String... args) {
        int goodCount = 130;
        int middleCount = 10;
        int badCount = 6;

        DianpingGrab dianpingGrab = new DianpingGrab();
        //获取好评数据
        List<Map<String, String>> goodDiscussMapList = new ArrayList<>();
        for (int i = 1; i <= goodCount; i++) {
            try {
                List<Map<String, String>> tempList = dianpingGrab.getDiscuss(i, "queryType=reviewGrade&queryVal=good");
                System.out.println("好评第" + i + "页检索完成,共" + tempList.size() + "条数据");
                goodDiscussMapList.addAll(tempList);
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //key:sheet名,value:sheet里的内容
        Map<String, List<Map<String, String>>> sheetMap = new HashMap<>(16);
        sheetMap.put("好评", goodDiscussMapList);

        //获取中评数据
        List<Map<String, String>> middleDiscussMapList = new ArrayList<>();
        for (int i = 1; i <= middleCount; i++) {
            try {
                List<Map<String, String>> tempList = dianpingGrab.getDiscuss(i,
                        "queryType=reviewGrade&queryVal=middle");
                System.out.println("中评第" + i + "页检索完成,共" + tempList.size() + "条数据");
                middleDiscussMapList.addAll(tempList);
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //key:sheet名,value:sheet里的内容
        sheetMap.put("中评", middleDiscussMapList);

        //获取差评数据
        List<Map<String, String>> badDiscussMapList = new ArrayList<>();
        for (int i = 1; i <= badCount; i++) {
            try {
                List<Map<String, String>> tempList = dianpingGrab.getDiscuss(i,
                        "queryType=reviewGrade&queryVal=bad");
                System.out.println("差评第" + i + "页检索完成,共" + tempList.size() + "条数据");
                badDiscussMapList.addAll(tempList);
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //key:sheet名,value:sheet里的内容
        sheetMap.put("差评", badDiscussMapList);
        ExcelUtils.writeToLocal("F:/南京海底世界-大众点评.xls",sheetMap);
    }

    /**
     * 获取评论信息
     *
     * @param pageNum 页码
     * @return 评论信息
     * @throws Exception 抛出异常
     */
    private List<Map<String, String>> getDiscuss(int pageNum, String param) throws Exception {
        Random random = new Random();
        char c = (char) (random.nextInt(26) + 97);
        //请求头
        Header[] headers = {
                new BasicHeader("Host", "www.dianping.com"),
                new BasicHeader("Connection", "keep-alive"),
                new BasicHeader("Upgrade-Insecure-Requests", "1"),
                new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"),
                new BasicHeader("Accept", "text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8"),
                new BasicHeader("Referer", "http://www.dianping.com/shop/1680115/review_all/p158"),
                new BasicHeader("Accept-Encoding", "gzip, deflate"),
                new BasicHeader("Accept-Language", "zh-Hans-CN, zh-Hans; q=0.8, en-US; q=0.5, en; q=0.3"),
                new BasicHeader("Cookie", "_hc.v=6c8416d6-ed8c-41b8-9ced-3a43e579fb32.1527573984; _lxsdk_cuid=163aa813269c8-0176162a736603-3a61430c-1fa400-163aa813269c0; _lxsdk=163aa813269c8-0176162a736603-3a61430c-1fa400-163aa813269c0; lgtoken=0e15c426c-572f-4d85-b0da-bf6f69913946; thirdtoken=1FB74AE487A088D94B1A6C145ECB8EA3; JSESSIONID=9725DD1F669F921B50CE9186507A0A43; dper=8ad91bf5e62111f02c2220677b6169df510b9d17a2b18431b65817483765a5e3c1be30b54dd6b6ad775a433c9dea7182ca422239dd0b679aa7387841262649ebfc3dc385b6cff1d618315fbd1c6ae396494d8f592240635d65b4e90f73a5c068; ll=7fd06e815b796be3df069dec7836c3df; ua=%E8%8B%A5%E6%9C%89_9465; ctu=cbb07ec445e5810531a8ad65ec50db9acac4620b7dbb9b2ba98010ad1f47dfdd; _lxsdk_s=163aa81326b-a65-310-ba8%7C%7C209")
        };
        String dianpingUrlStr = "http://www.dianping.com/shop/1680115/review_all/p1";
        System.out.println(dianpingUrlStr);
        Spider spider = new Spider();
        //获取页面数据
        String pageContentStr = spider.getPageByGet(dianpingUrlStr, headers);
        SightRegex sightRegex = new SightRegex();
        return sightRegex.dianping(pageContentStr, pageNum);
    }
}
